package cc.yugu.yhdw.webserver.web.controller.security.hdw;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.domain.basic.Agent;
import cc.yugu.yhdw.common.domain.basic.Platform;
import cc.yugu.yhdw.common.domain.hdw.Agency;
import cc.yugu.yhdw.common.domain.hdw.ExchangeAmountMoneyDivideDetail;
import cc.yugu.yhdw.common.domain.hdw.Supplier;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.common.utils.YhdwUtils;
import cc.yugu.yhdw.webserver.persistence.basic.AgentMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.AgencyMapper;
import cc.yugu.yhdw.webserver.service.basic.AgentService;
import cc.yugu.yhdw.webserver.service.basic.PlatformService;
import cc.yugu.yhdw.webserver.service.hdw.AgencyService;
import cc.yugu.yhdw.webserver.service.hdw.ExchangeAmountMoneyDivideDetailService;
import cc.yugu.yhdw.webserver.service.hdw.SupplierService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/security/hdw/exchange_amount_money_divide_detail")
public class ExchangeAmountMoneyDivideDetailController extends SecurityController {
    @Autowired
    ExchangeAmountMoneyDivideDetailService exchangeAmountMoneyDivideDetailService;
    @Autowired
    SupplierService supplierService;
    @Autowired
    AgentService agentService;
    @Autowired
    AgencyService agencyService;
    @Autowired
    PlatformService platformService;

    @RequestMapping(value = "page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(ExchangeAmountMoneyDivideDetail exchangeAmountMoneyDivideDetail) {
        return PageResult.successResult(exchangeAmountMoneyDivideDetailService.findPage(exchangeAmountMoneyDivideDetail));
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "unique.htm")
    public ExtResult unique(ExchangeAmountMoneyDivideDetail exchangeAmountMoneyDivideDetail) {
        if (exchangeAmountMoneyDivideDetail.getAmount()==null||exchangeAmountMoneyDivideDetail.getAmount()==0||exchangeAmountMoneyDivideDetail.getDivideId()==null||exchangeAmountMoneyDivideDetail.getDivideId()==0)
            return ExtResult.successResult();
        int percent = exchangeAmountMoneyDivideDetailService.unique(exchangeAmountMoneyDivideDetail);
        if (percent + exchangeAmountMoneyDivideDetail.getPercent()>100) {
            return ExtResult.failResult("同一换电次数分配比例最大为100%");
        }
        return ExtResult.successResult();
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping("add.htm")
    public String add(Integer divideId, Model model) {
        model.addAttribute("divideId", divideId);
        model.addAttribute("orgTypeList", ExchangeAmountMoneyDivideDetail.OrgType.values());
        return "/security/hdw/exchange_amount_money_divide_detail/add";
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "edit.htm")
    public String edit(int id, Model model) {
        ExchangeAmountMoneyDivideDetail exchangeAmountMoneyDivideDetail = exchangeAmountMoneyDivideDetailService.find(id);
        model.addAttribute("entity", exchangeAmountMoneyDivideDetail);
        return "/security/hdw/exchange_amount_money_divide_detail/edit";
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "create.htm")
    public ExtResult create(ExchangeAmountMoneyDivideDetail entity) {
        return exchangeAmountMoneyDivideDetailService.insert(entity);
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "update.htm")
    public ExtResult update(ExchangeAmountMoneyDivideDetail entity) {
        return exchangeAmountMoneyDivideDetailService.update(entity);
    }

    @RequestMapping("delete.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult delete(int id) {
        exchangeAmountMoneyDivideDetailService.delete(id);
        return ExtResult.successResult();
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "edit_orgType.htm", produces = "text/html;charset=UTF-8")
    public String edit_orgType(Integer id) throws IOException {
        return select_orgType(id);
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "add_orgType.htm", produces = "text/html;charset=UTF-8")
    public String add_orgType() throws IOException {
        return select_orgType(null);
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "edit_orgId.htm", produces = "text/html;charset=UTF-8")
    public String edit_orgId(Integer id, Integer typeId, HttpSession httpSession) throws IOException {
        Integer agentId = getSessionUser(httpSession).getAgentId();
        Integer agencyId = getSessionUser(httpSession).getAgencyId();
        return select_orgId(id, typeId, agentId, agencyId);
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "add_orgId.htm", produces = "text/html;charset=UTF-8")
    public String add_orgId(Integer typeId, HttpSession httpSession) throws IOException {
        Integer agentId = getSessionUser(httpSession).getAgentId();
        Integer agencyId = getSessionUser(httpSession).getAgencyId();
        return select_orgId(null, typeId, agentId, agencyId);
    }

    private String select_orgType(Integer id) throws IOException {
        ExchangeAmountMoneyDivideDetail.OrgType[] orgTypes = ExchangeAmountMoneyDivideDetail.OrgType.values();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = null;
        for (ExchangeAmountMoneyDivideDetail.OrgType orgType : orgTypes) {
            map = new HashMap<String, Object>();
            map.put("id", orgType.getValue());
            map.put("text", orgType.getName());
            if (id == null && list.size() == 0 || Integer.valueOf(orgType.getValue()) == id) {
                map.put("selected", true);
            }
            list.add(map);
        }
        return YhdwUtils.encodeJson(list);
    }

    private String select_orgId(Integer id, Integer typeId, Integer agentId, Integer agencyId) throws IOException {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = null;
        if (typeId == ExchangeAmountMoneyDivideDetail.OrgType.HQ.getValue()) {
            map = new HashMap<String, Object>();
            Platform system = platformService.find();
            map.put("id", system.getId());
            map.put("text", system.getPlatformName());
            map.put("selected", true);
            list.add(map);
        } else if (typeId == ExchangeAmountMoneyDivideDetail.OrgType.CABINET.getValue()) {
            List<Supplier> suppliers = supplierService.findByType(Supplier.Type.CABINET.getValue());
            for (Supplier supplier : suppliers) {
                map = new HashMap<String, Object>();
                map.put("id", supplier.getId());
                map.put("text", supplier.getSupplierName());
                if (id == null && list.size() == 0 || supplier.getId() == id) {
                    map.put("selected", true);
                }
                list.add(map);
            }
        } else if (typeId == ExchangeAmountMoneyDivideDetail.OrgType.POWER.getValue()) {
            List<Supplier> suppliers = supplierService.findByType(Supplier.Type.POWER.getValue());
            for (Supplier supplier : suppliers) {
                map = new HashMap<String, Object>();
                map.put("id", supplier.getId());
                map.put("text", supplier.getSupplierName());
                if (id == null && list.size() == 0 || supplier.getId() == id) {
                    map.put("selected", true);
                }
                list.add(map);
            }
        } else if (typeId == ExchangeAmountMoneyDivideDetail.OrgType.BATTERY.getValue()) {
            List<Supplier> suppliers = supplierService.findByType(Supplier.Type.BATTERY.getValue());
            for (Supplier supplier : suppliers) {
                map = new HashMap<String, Object>();
                map.put("id", supplier.getId());
                map.put("text", supplier.getSupplierName());
                if (id == null && list.size() == 0 || supplier.getId() == id) {
                    map.put("selected", true);
                }
                list.add(map);
            }
        } else if (typeId == ExchangeAmountMoneyDivideDetail.OrgType.AGENCY.getValue()) {
            if (agencyId == null || agencyId == 0) {
                List<Agency> agencyList = agencyService.findByAgent(agencyId);
                for (Agency agency : agencyList) {
                    map = new HashMap<String, Object>();
                    map.put("id", agency.getId());
                    map.put("text", agency.getAgencyName());
                    if (id == null && list.size() == 0 || agency.getId() == id) {
                        map.put("selected", true);
                    }
                    list.add(map);
                }
            } else {
                map = new HashMap<String, Object>();
                Agency agency = agencyService.find(agencyId);
                map.put("id", agency.getId());
                map.put("text", agency.getAgencyName());
                map.put("selected", true);
                list.add(map);
            }
        } else if (typeId == ExchangeAmountMoneyDivideDetail.OrgType.AGENT.getValue()) {
            if (agentId == null || agentId == 0) {
                List<Agent> agentList = agentService.findAll();
                for (Agent agent : agentList) {
                    map = new HashMap<String, Object>();
                    map.put("id", agent.getId());
                    map.put("text", agent.getAgentName());
                    if (id == null && list.size() == 0 || agent.getId() == id) {
                        map.put("selected", true);
                    }
                    list.add(map);
                }
            } else {
                map = new HashMap<String, Object>();
                Agent agent = agentService.find(agentId);
                map.put("id", agent.getId());
                map.put("text", agent.getAgentName());
                map.put("selected", true);
                list.add(map);
            }
        }
        if (list.size() == 0) {
            map = new HashMap<String, Object>();
            map.put("id", null);
            map.put("text", null);
            map.put("selected", true);
            list.add(map);
        }
        return YhdwUtils.encodeJson(list);
    }
}
